package com.yingdong.tools;

import java.io.FileWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class DBSchemaGenerator {

	public static String transDBTypeToJavaType(String dbType) {
		Map<String, String> map = new HashMap<>();
		map.put("VARCHAR", "String");
		map.put("CHAR", "String");
		map.put("TEXT", "String");
		map.put("CHAR", "String");
		map.put("INT", "Integer");
		map.put("TINYINT", "Integer");
		map.put("SMALLINT", "Integer");
		map.put("MEDIUMINT", "Integer");
		map.put("INTEGER", "Long");
		map.put("ID", "Long");
		map.put("FLOAT", "Float");
		map.put("DOUBLE", "Double");
		map.put("BIGINT", "Long");
		return map.get(dbType);
	}

	public static void main(String[] args) throws Exception {
		Configuration cfg = new Configuration();
		cfg.setDefaultEncoding("UTF-8");
		// 设置模板文件所在的目录
		cfg.setClassForTemplateLoading(ExcelTemplateGenerator.class, "/template");
		Writer out = null;

		// 取得模板文件
		Template t = cfg.getTemplate("DBSchema.ftl");

		Class.forName("com.mysql.jdbc.Driver").newInstance();
		Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.0.104:3306/ydb?user=root&password=123456");
		DatabaseMetaData m_DBMetaData = conn.getMetaData();
		ResultSet tableRet = m_DBMetaData.getTables(null, "%", "%", new String[] { "TABLE" });
		while (tableRet.next()) {
			String tableName = tableRet.getString("TABLE_NAME");
			tableName=tableName.substring(0, 1).toUpperCase() + tableName.substring(1);
			ResultSet colRet = m_DBMetaData.getColumns(null, "%", tableName, "%");
			Map<String, DBSchemaField> typeMap = new HashMap<>();
			List<DBSchemaField> fields = new ArrayList<>();
			List<DBSchemaField> keys = new ArrayList<>();
			while (colRet.next()) {
				String columnName = colRet.getString("COLUMN_NAME");
				String columnType = transDBTypeToJavaType(colRet.getString("TYPE_NAME"));
				String comment = colRet.getString("REMARKS");
				int datasize = colRet.getInt("COLUMN_SIZE");
				int digits = colRet.getInt("DECIMAL_DIGITS");
				int nullable = colRet.getInt("NULLABLE");
				String autoIncreament=colRet.getString("IS_AUTOINCREMENT");
				DBSchemaField schema = new DBSchemaField(columnType, comment, columnName, false,autoIncreament.equals("YES"));
				typeMap.put(columnName, schema);
				fields.add(schema);
				
			}
			ResultSet ids = m_DBMetaData.getPrimaryKeys(null, "%", tableName);
			while (ids != null && ids.next()) {
				String col = ids.getString("COLUMN_NAME");
				typeMap.get(col).setPrim(true);
				keys.add(typeMap.get(col));
			}
			String fileName = tableName;
			Map root = new HashMap();

			root.put("fields", fields);
			root.put("classname", fileName);
			root.put("keys", keys);
			out = new FileWriter("src/main/java/com/yingdong/db/" + fileName + ".java");
			t.process(root, out);
		}

	}

}
